﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using mail = System.Net.Mail;

public class SendEmails : UserControl
{
    string str, UserID;
    string HTMLFilePath = string.Empty;
    Mailer mailer;

    SqlConnection con = new SqlConnection();
    SqlDataReader rd;
    string strError = string.Empty;
    private mail.MailMessage MailObject;
	public SendEmails()
	{
        mailer = new Mailer();
	}

    private string RenderFileNew()
    {
        StringBuilder sb = new StringBuilder();
        if (!string.IsNullOrEmpty(HTMLFilePath) || (HTMLFilePath != ""))
        {
            string tempStringLine = string.Empty;

            var control = this.LoadControl(HTMLFilePath);
            using (var sw = new StringWriter())
            {
                control.RenderControl(new HtmlTextWriter(sw));
                tempStringLine = sw.ToString();
            }

            sb.Append(tempStringLine);
        }
        // sr.Dispose();
        return sb.ToString();
    }

    public void GeneralEmail(string Subject, string Email, int TemplateID)
    {
        mailer.MailFrom = "sendemails209@gmail.com";
        mailer.MailTo = Email;


        mailer.SMTPUsername = "sendemails209";
        mailer.SMTPPassword = "nillvhoftetjqhxm";
        mailer.SMTPServer = "smtp.gmail.com";
        mailer.SMTPSSL = true;
        mailer.SMTPPort = 25;

        mailer.MailSubject = Subject;
        switch (TemplateID)
        {
            case 1: HTMLFilePath = "~/EMailTemplate/Registration.ascx"; break;
            case 2: HTMLFilePath = "~/EMailTemplate/PasswordRecoveryTemplate.ascx"; break;
            case 3: HTMLFilePath = "~/EMailTemplate/Reference.ascx"; break;
            case 4: HTMLFilePath = "~/EMailTemplate/ReminderMail.ascx"; break;
            case 5: HTMLFilePath = "~/EMailTemplate/verifypayment.ascx"; break;
            case 6: HTMLFilePath = "~/EMailTemplate/AdReply.ascx"; break;
            case 7: HTMLFilePath = "~/EMailTemplate/NewMessage.ascx"; break;
        }
        mailer.MailBody = RenderFileNew();
        try
        {
            bool gone = mailer.Send();
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
    }

    RegistrationData reg = new RegistrationData();
    public void SendEmail(string SenderName, int UserID)
    {
        EmailData.From = SenderName;
        DataSet dsReceiver = new DataSet();
        dsReceiver = reg.GetUserID(UserID);
        if (!object.Equals(dsReceiver, null))
        {
            if (dsReceiver.Tables.Count > 0)
            {
                if (dsReceiver.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = dsReceiver.Tables[0].Rows[0];
                    EmailData.Name = Convert.ToString(dr["FirstName"]);
                    if (!string.IsNullOrEmpty(Convert.ToString(dr["Email"])))
                        GeneralEmail("New Message", Convert.ToString(dr["Email"]), 7);//New Message
                }
            }
        }
    }
      
}